-- Pierwotny właściciel i twórca kodu: AmaMa
-- Zakaz używania / edycji kodu bez wcześniejszej zgody ze strony właściciela
-- Zakaz wykorzystywania całego lub fragmentów kodu
-- W przypadku złamania powyzszych zasad sprawa zostanie skierowana do sądu pod zarzutem: Naruszenia majątkowych praw autorskich
-- Pierwotnie pełne prawo do używania tego kodu posiada tylko AmaMa
mojeW,mojeH = 1360,768
sW,sH = guiGetScreenSize()
sx, sy = (sW/mojeW), (sH/mojeH)

local wFont = dxCreateFont("OSB.ttf", 15*sx)
local wFont2 = dxCreateFont("OSB.ttf", 11*sx)
data={}
data[localPlayer]={}
data[localPlayer]["total"]=0
data[localPlayer]["actually"]=0
data[localPlayer]["percent"]=0
data[localPlayer]["znak"]="kB"
data[localPlayer]["znak2"]="kB"

function render()
    local sin=math.sin(getTickCount()/2000)*40
	dxDrawImage(0*sx, 0*sy, 1360*sx, 768*sy, "files/background.jpg", 0, 0, 0, tocolor(255, 255, 255, 255), false)
    local soundFFT = getSoundFFTData ( sound, 2048, 2 )
	if ( soundFFT ) then
		for i,v in ipairs(soundFFT) do
		local pos1 = (500*sx)-(math.sqrt ( soundFFT[i]*1024 ))*2
		local pos2 = (50*sy)-(math.sqrt ( soundFFT[i]*1024 ))*2
		local pos3 = (366*sx)+(math.sqrt ( soundFFT[i]*1024 ))*4
		local pos4 = (368*sy)+(math.sqrt ( soundFFT[i]*1024 ))*4
		dxDrawImage ( pos1,pos2,pos3,pos4, image, angle, 0, -120 )
        end
	end
    setTransferBoxVisible(true)
    dxDrawText("Trwa pobieranie zasobów serwera...\n("..(data[localPlayer]["percent"]).."%)",0*sx,500*sy,1360*sx,168*sy, tocolor(255, 255, 255, 255), 1.00, wFont , "center", "bottom", false, false, true, false, false)
  -- dxDrawText(math.floor(data[localPlayer]["actually"])..""..data[localPlayer]["znak"].."/"..math.floor(data[localPlayer]["total"])..""..data[localPlayer]["znak2"].." ("..data[localPlayer]["percent"].."%)",0*sx,600*sy,1360*sx,168*sy , tocolor(255, 255, 255, 255), 1.00,wFont , "left", "center", false, false, true, false, false)

    --[[
    dxDrawRectangle(pos["rec"][1],pos["rec"][2],pos["rec"][3],pos["rec"][4], tocolor(0, 0, 0, 255), false)
    dxDrawImage(pos["bg"][1]+sin,pos["bg"][2],pos["bg"][3],pos["bg"][4], "img/"..images[1][tonumber(data[localPlayer]["count"])], 0, 0, 0, tocolor(255, 255, 255, data[localPlayer]["alpha"]), false)
    dxDrawImage(pos["load"][1],pos["load"][2],pos["load"][3],pos["load"][4], "img/load.png", getTickCount()/10, 0, 0, tocolor(255, 255, 255, 255), true)
    dxDrawText(math.floor(data[localPlayer]["actually"])..""..data[localPlayer]["znak"].."/"..math.floor(data[localPlayer]["total"])..""..data[localPlayer]["znak2"].." ("..data[localPlayer]["percent"].."%)",pos["txt_download"][1],pos["txt_download"][2],pos["txt_download"][3],pos["txt_download"][4] , tocolor(255, 255, 255, 255), 1.00,data[localPlayer]["font"][2] , "left", "center", false, false, true, false, false)
    ]]
end


function downloadComplete()
    if not isTransferBoxActive() then
        removeEventHandler("onClientRender", root, render)
        stopSound(sound)
        destroyElement(image)
        showChat(true)
    else
        setTimer(downloadComplete, 1000, 1)
    end
end


addEventHandler("onClientResourceStart", resourceRoot, function()
    addEventHandler("onClientRender", root, render)
    image = dxCreateTexture( "files/logo.png" ,"argb", false, "clamp" )
    sound=playSound("files/sound.mp3",true)
    setSoundVolume(sound,0.5)
    setTimer(downloadComplete, 1000, 1)
    showChat(false)
end)

addEventHandler("onClientTransferBoxProgressChange", root, function(actually, total)
    data[localPlayer]["percent"]=math.floor(((actually*2) / total) * 100)-9
    if data[localPlayer]["percent"] < 0 then data[localPlayer]["percent"] = 0 end
    if data[localPlayer]["percent"] > 100 then data[localPlayer]["percent"] = 100 end
    if (actually/1024)/1024<1 then
        data[localPlayer]["actually"]=actually/1024
        data[localPlayer]["znak"]="kB"
    elseif (actually/1024)/1024>=1 then
        data[localPlayer]["actually"]=(actually/1024)/1024
        data[localPlayer]["znak"]="MB"
    end

    if (total/1024)/1024<1 then
        data[localPlayer]["total"]=total/1024
        data[localPlayer]["znak2"]="kB"
    elseif (total/1024)/1024>=1 then
        data[localPlayer]["total"]=(total/1024)/1024
        data[localPlayer]["znak2"]="MB"
    end
end)